package com.hyfrogx.modules.sys.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hyfrogx.common.annotation.SysLog;
import com.hyfrogx.common.base.QueryForm;
import com.hyfrogx.common.utils.R;
import com.hyfrogx.common.validator.group.AddGroup;
import com.hyfrogx.common.validator.group.UpdateGroup;
import com.hyfrogx.modules.sys.entity.SysDictEntity;
import com.hyfrogx.modules.sys.form.dict.DictSaveForm;
import com.hyfrogx.modules.sys.form.dict.DictUpdateForm;
import com.hyfrogx.modules.sys.form.dict.DictVo;
import com.hyfrogx.modules.sys.service.SysDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * <p>
 * 字典 前端控制器
 * </p>
 *
 * @author wanglei
 * @date 2019-07-15 15:51:11
 */
@RestController
@RequestMapping("/sys/dict")
@ApiIgnore
@Api(value = "字典管理controller", tags = {"sys--字典"})
public class SysDictController extends AbstractController {

	@Autowired
	private SysDictService dictService;

	@ApiOperation("字典信息")
	@PostMapping("/info/{id}")
	@RequiresPermissions("sys:dict:info")
	public R info(@PathVariable("id") Long id) {
		DictVo vo = dictService.queryVoById(id, DictVo.class);
		return R.ok().put("data", vo);
	}

	@ApiOperation("字典列表")
	@PostMapping("/select")
	@RequiresPermissions("sys:dict:select")
	public R select(@RequestBody QueryForm<SysDictEntity> form) {
		//只查启用的
		form.setParams("status",1);
		List<DictVo> list = dictService.queryVoList(
				dictService.getSelectWrapper(form, true)
				, DictVo.class
		);
		return R.ok().put("list", list);
	}

	@ApiOperation("字典列表")
	@PostMapping("/list")
	@RequiresPermissions("sys:dict:list")
	public R list(@RequestBody QueryForm<SysDictEntity> form) {
		IPage page = dictService.queryVoPage(
				form.getIPage(),
				dictService.getSelectWrapper(form, false)
				, DictVo.class
		);
		return R.ok().put("page", page);
	}

	@ApiOperation("字典新建")
	@SysLog("保存字典")
	@PostMapping("/save")
	@RequiresPermissions("sys:dict:save")
	public R save(@RequestBody @Validated(AddGroup.class) DictSaveForm form) {
		if (ObjectUtil.isNotNull(dictService.saveForm(getUserId(), form))) {
			return R.ok();
		}
		return R.error("保存字典失败");
	}

	@ApiOperation("字典更新")
	@SysLog("更新字典")
	@PostMapping("/update")
	@RequiresPermissions("sys:dict:update")
	public R update(@RequestBody @Validated(UpdateGroup.class) DictUpdateForm form) {
		if (ObjectUtil.isNotNull(dictService.updateForm(getUserId(), form))) {
			return R.ok();
		}
		return R.error("修改字典失败");
	}

	@ApiOperation("字典启用")
	@SysLog("启用字典")
	@PostMapping("/enable")
	@RequiresPermissions("sys:dict:enable")
	public R enable(@RequestBody List<Long> ids) {
		if (dictService.enableByIdList(getUserId(), ids)) {
			return R.ok();
		}
		return R.error("启用字典失败");
	}

	@ApiOperation("字典禁用")
	@SysLog("禁用字典")
	@PostMapping("/disable")
	@RequiresPermissions("sys:dict:disable")
	public R disable(@RequestBody List<Long> ids) {
		if (dictService.disableByIdList(getUserId(), ids)) {
			return R.ok();
		}
		return R.error("禁用字典失败");
	}


	@ApiOperation("字典删除")
	@SysLog("删除字典")
	@PostMapping("/delete")
	@RequiresPermissions("sys:dict:delete")
	public R delete(@RequestBody List<Long> ids) {
		if (dictService.deleteByIdList(getUserId(), ids)) {
			return R.ok();
		}
		return R.error("删除字典失败");
	}
}

